using System.Collections;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;

public class DeepSeekApiClient : MonoBehaviour
{
    private string apiKey = "sk-f6f17dc919d94bc297c998953d7991e4";
    private string baseUrl = "https://api.deepseek.com/chat/completions";
    private string model = "deepseek-chat";
    private string systemMessage = "You are a helpful assistant.";
    private string userMessage = "Hello!";

    void Start()
    {
        StartCoroutine(GetCompletionAsync());
    }

    IEnumerator GetCompletionAsync()
    {
        var requestBody = new
        {
            model = model,
            messages = new[]
            {
                new { role = "system", content = systemMessage },
                new { role = "user", content = userMessage }
            },
            stream = false
        };

        string jsonRequest = JsonUtility.ToJson(requestBody);
        byte[] jsonBytes = Encoding.UTF8.GetBytes(jsonRequest);

        using (UnityWebRequest www = new UnityWebRequest(baseUrl, "POST"))
        {
            www.uploadHandler = new UploadHandlerRaw(jsonBytes);
            www.downloadHandler = new DownloadHandlerBuffer();
            www.SetRequestHeader("Content-Type", "application/json");
            www.SetRequestHeader("Authorization", $"Bearer {apiKey}");

            yield return www.SendWebRequest();

            if (www.result != UnityWebRequest.Result.Success)
            {
                Debug.LogError($"Request error: {www.error}");
            }
            else
            {
                Debug.Log("Response from DeepSeek API:");
                Debug.Log(www.downloadHandler.text);
            }
        }
    }
}